package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.model.SortModel;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CustomChooseGradePopup extends PartShadowPopupView {

    @BindView(R.id.nsgv_grade_grid)
    NonSlidingGridView nsgvGradeGrid;
    private Unbinder mUnBinder;
    private int chooseIndex;
    private List<DictInfo> gradData;
    private CommonAdapter<DictInfo> gradAdapter;
    private ICustomChooseGradePopupListener listener;

    public CustomChooseGradePopup(@NonNull Context context, List<DictInfo> gradData) {
        super(context);
        this.gradData = gradData;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_choose_grade_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
        nsgvGradeGrid.setAdapter(gradAdapter = new CommonAdapter<DictInfo>(getContext(), R.layout.item_sales_ranking_type, gradData) {
            @Override
            protected void convert(ViewHolder viewHolder, DictInfo item, int position) {
                viewHolder.setImageResource(R.id.iv_item_icon, item.getIcon());
                viewHolder.setText(R.id.tv_item_name, item.getValue());
                viewHolder.setTextColor(R.id.tv_item_name, ContextCompat.getColor(getContext(), chooseIndex == position ? R.color.app_color : R.color.c_333333));
            }
        });
        nsgvGradeGrid.setOnItemClickListener((parent, view, position, id) -> {
            if(chooseIndex == position){
                return;
            }
            chooseIndex = position;
            gradAdapter.notifyDataSetChanged();
            dismiss();
            if (listener != null) {
                listener.onChooseGrade(gradData.get(position));
            }
        });
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    public void setListener(ICustomChooseGradePopupListener listener) {
        this.listener = listener;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    public interface ICustomChooseGradePopupListener {
        void onChooseGrade(DictInfo grade);
    }

}
